exports.Login = function (req, res) {

    var bodyParams = req.routeInfo.bodyParams;

    //最终结果
    var resultData = {};

    var userInfo = [];

    var oa_uid = 0;

    resultData.is_root = 0;

    resultData.is_header = 0;

    var userRole = 0;

    var interfaceId = [];

    //任务流创建
    async.waterfall([
            //查询管理员openid
            function (cb) {

                console.log("0000000")

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOrganizationUser + "/ou_uid/" + bodyParams.u_id,
                        method: 'POST',
                        data: {
                            fields: ["openid", "ou_uid", "ou_id as m_ou_id", "ou_phone as m_phone"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        //返回登录成功后的数据
                        resultData.organizationUserData = data.data[0];
                        cb();


                    }
                );

            },
            //查询管理员openid
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.organizationSearchArtProductOrder + "/apo_oid/" + bodyParams.u_oid + "/skip/0/limit/1",
                        method: 'POST',
                        data: {
                            fields: ["apo_phone"].join(",")
                        }
                    },
                    function (err, data) {
                        resultData.userPhone = data.data;
                        cb();
                    }
                );
            },

            //查询机构的信息
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOrganization + "/o_id/" + bodyParams.u_oid,
                        method: 'POST',
                        data: {
                            fields: ["o_uid", "o_id", "o_name", "o_logo", "o_slogan", "o_address", "o_phone"].join(",")
                        }
                    },
                    function (err, data) {

                        console.log(11111)
                        console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        //返回登录成功后的数据
                        resultData.organizationInfo = data.data;
                        cb();


                    }
                );

            },

            //查询购买信息
            function (cb) {

                var nowTime = common.nowTime(false);

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.organizationSearchArtPeoductRecord + "/apur_oid/" + bodyParams.u_oid + "/between/" + nowTime + "-apur_end_time",
                        method: 'POST',
                        data: {
                            fields: "apur_apid,apur_end_time"
                        }
                    },
                    function (err, data) {

                        console.log(222222)
                        console.log(data);

                        var pstr = [];
                        var endTime = {};

                        if (data.data.length > 0) {

                            for (var i = 0; i < data.data.length; i++) {
                                endTime[data.data[i].apur_apid] = data.data[i].apur_end_time
                                pstr.push(data.data[i].apur_apid);
                            }

                            resultData.permission = pstr;
                            resultData.endTime = endTime;

                        }

                        cb();
                    }
                );


            },

            function (cb) {

                console.log("^^^^^^^^^^")
                console.log("http://127.0.0.1:3500/erpUser/search/ou_username/" + bodyParams.u_username)

                R.SEND_HTTP(
                    req,
                    {
                        url: "http://127.0.0.1:3500/erpUser/search/ou_username/" + bodyParams.u_username,
                        method: 'POST',
                        data: {
                            fields: ["ou_id", "ou_oesid", "ou_name", "ou_username", "ou_organization_user_id"].join(",")
                        }
                    },
                    function (err, data) {
                        console.log("3333333")
                        console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        if (data.data.length > 0) {
                            resultData.ou_id = data.data[0].ou_id;
                            resultData.ou_oesid = data.data[0].ou_oesid;
                            resultData.ou_name = data.data[0].ou_name;
                            resultData.ou_username = data.data[0].ou_username;
                            resultData.m_userid = data.data[0].ou_organization_user_id;
                        } else {
                            resultData.ou_id = 0;
                            resultData.ou_oesid = 0;
                            resultData.ou_name = 0;
                            resultData.ou_username = 0;
                            resultData.m_userid = 0;
                        }
                        cb();


                    }
                );

            },

            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: "http://127.0.0.1:3500/erpUser/search/ou_organization_user_id/" + bodyParams.u_id,
                        method: 'POST',
                        data: {
                            fields: ["ou_id", "ou_type", "ou_oesid"].join(",")
                        }
                    },
                    function (err, data) {
                        console.log(444444)
                        console.log(data);

                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        if (data.data.length <= 0) {
                            resultData.oa_uid = 0;
                            resultData.ou_type = 0;
                        } else {
                            resultData.oa_uid = data.data[0].ou_id;
                            resultData.ou_type = data.data[0].ou_type;
                            oa_uid = data.data;
                        }

                        cb();
                    }
                );
            },

            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: "http://127.0.0.1:3500/erpAdministrativeFinance/search_enterprise_structure_user_info/oesum_uid/" + oa_uid[0].ou_id,
                        method: 'POST',
                        data: {
                            fields: ["oesum_oesid", "oes_name", "oes_department", "oes_is_root"].join(",")
                        }
                    },
                    function (err, data) {

                        console.log("/*--*//*--//*--*/");
                        console.log(data);

                        var department = "";

                        if (data.data.length > 0) {

                            for (var i = 0; i < data.data.length; i++) {
                                department += data.data[i].oes_department + ",";
                                if (data.data[i].oesum_oesid == oa_uid[0].ou_oesid) {
                                    resultData.is_header = 1;
                                    break;
                                }

                                if (data.data[i].oes_is_root == 1) {
                                    resultData.is_root = 1;
                                }
                            }
                            department = department.substring(0, department.length - 1);

                        }


                        if (resultData.is_header == 1) {

                            department = "管理员";

                        } else {

                            if (data.data.length > 0) {
                                console.log("<`><`><`>");
                                department = "未设置部门";
                                if (data.data[0].oes_department != "" || data.data[0].oes_department != null) {
                                    department = data.data[0].oes_department;
                                }
                            } else {
                                console.log("<><><>");
                                department = "未设置部门"
                            }

                        }

                        console.log("48*-/-/");
                        console.log(department);

                        resultData.department = department;

                        resultData.department = department.length <= 0 ? "未设置部门" : department;

                        cb();


                    }
                );
            },

            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: "http://127.0.0.1:3400/product_version/search",
                        method: 'POST',
                        data: {
                            fields: ["pv_product_name", "pv_number", "pv_id"].join(",")
                        }
                    },
                    function (err, data) {

                        if (err) {
                            return cb(err, data);
                        }

                        resultData.productVersion = data.data;

                        cb();
                    }
                );
            },
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: "http://127.0.0.1:3500/erpUser/searchRole/our_ouid/" + resultData.ou_id,
                        method: 'POST',
                    },
                    function (err, data) {

                        if (err) {
                            return cb(err, data);
                        }

                        userRole = data.data;
                        console.log("userRole----------------------------*");
                        console.log(userRole);
                        cb();
                    }
                );
            },
            function (cb) {

                //创建添加任务
                var tasks = [];

                //循环节点列表
                userRole.forEach(function (item, i) {

                    //创建每一个插入任务
                    tasks.push(function (cb) {

                        //查询对该用户的回复
                        R.SEND_HTTP(
                            req,
                            {
                                url: "http://127.0.0.1:3500/erpPermission/search/op_orid/" + item.our_orid,
                                method: 'POST',
                                data: {
                                    fields: ["oi_id"].join(",")
                                }
                            },
                            function (err, data) {

                                if (data.data.length > 0) {
                                    for (var i = 0; i < data.data.length; i++) {
                                        interfaceId.push(data.data[i].oi_id)
                                    }
                                }
                                cb();
                            });
                    });
                });
                async.waterfall(tasks, cb);

            },
            function (cb) {
                console.log("interfaceId");
                console.log(interfaceId);
                var enrollmentTreasureIsReady = 0;

                var officialWebsiteAndOnlineSchool_isReady = 0;

                for (var i = 0; i < interfaceId.length; i++) {
                    if (interfaceId[i] == 186) {
                        enrollmentTreasureIsReady = 1;
                    } else if (interfaceId[i] == 187) {
                        officialWebsiteAndOnlineSchool_isReady = 1;
                    }
                }
                resultData.enrollmentTreasureIsReady = enrollmentTreasureIsReady;
                resultData.officialWebsiteAndOnlineSchool_isReady = officialWebsiteAndOnlineSchool_isReady;

                cb()
            },
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: "http://127.0.0.1:3500/erpUser/permissionQuery",
                        method: 'POST',
                        data: {
                            oId: bodyParams.u_oid,
                            userId: resultData.ou_id,
                        }
                    },
                    function (err, data) {

                        if (err) {
                            return cb(err, data);
                        }

                        resultData.permissionData = data;
                        cb();
                    }
                );
            }
        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );

}